{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Styles, Scales, and Shapes - 🐭" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Styles and Shapes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Styling* - The styling is handled through something call CSS (Cascading Style Sheets), but we can specifically handle this within our D3.js code. Here is a list of typical style changes. \n", "* fill - the color inside the shape\n", "* stroke - the border of the shape\n", "* opacity - the transparency of the shape\n", "* Note: there are more combinations, but these are the basics and covers a good amount of styling.\n", "\n", "*Shapes* - At this point, we have been using circles primarily for out designs. There are few other shapes we need to cover and what is needed to draw these.\n", "* Circles (\"circle\") - cx, cy, r\n", "* Rectangles (\"rect\") - x, y, width, height\n", "* Line (\"line\") - x1, y1, x2, y2\n", "* Text (\"text\") - x, y\n", "* Paths (\"path\") - these are by far the most complicate shapes, and will require further discussion throughout these notebooks\n", "* Note: There are ellipse, polylines, and polygons, but these are RARELY used in D3.js\n", ". " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First things, first... Let's bring back our last project for Part 2." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```javascript\n", "var dataset = [3, 5, 5, 6, 15, 18]\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n" ], "text/plain": [ "So, when to use d3.extent or d3.min/d3.max? This is a good example of this case. Right now,
\n", "\n", "\n", "```javascript\n", "const color = d3.scaleLinear().range([0,1]).domain(d3.extent(dataset))\n", "```\n", "\n", "\n", "assumes that our lowest number, 3, is mapped to 0. Though, in some cases, we want 0 to be mapped to 0. Meaning we should use
\n", "\n", "\n", "```javascript\n", "const color = d3.scaleLinear().range([0,1]).domain([0,d3.max(dataset)])\n", "```" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n" ], "text/plain": [ ".text((d,i) => \"x: \"+d+\" y: \"+i)
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the \"x: \", the plus sign (+), and d will combine or concatenate the two to make one string"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The last thing that needs to be adjusted is the fact that both the rectangle and the text occupy the same x,y coordinate, which means they overlap a bit. Also, the text for our last rectangle is off the canvas. So, let's adjust both of these."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n"
],
"text/plain": [
"